<?php

namespace App\Http\Controllers\Admin;

use App\Contracts\Repositories\NewsCategoryRepository;
use App\Contracts\Repositories\NewsRepository;
use App\Http\Requests\Admin\NewsCreateRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;

class NewsController extends Controller
{
    private $newsCategoryRepository;
    private $repositry;

    public function __construct(NewsCategoryRepository $newsCategoryRepository,NewsRepository $repository)
    {
        $this->newsCategoryRepository = $newsCategoryRepository;
        $this->repositry= $repository;
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $rows = $this->repositry->orderBy("id","desc")->paginate(2);
        return view("admin.news.index",["rows"=>$rows]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $newsCategories = $this->newsCategoryRepository->all(["id","name","parent_id"]);
        $newsCategories=  $this->newsCategoryRepository->sort($newsCategories);
        return view("admin.news.create",["newsCategories"=>$newsCategories]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(NewsCreateRequest $request)
    {
        $requestData = $request->all();

        if($request->hasFile("page_image")){
            $path = $request->page_image->store("news");
            $requestData['page_image'] = $path;
        }

       if($this->repositry->create($requestData)){
            return Redirect::to("admin/news");
       }else{
            return Redirect::back()->withInput()->withErrors("保存失败！");
       }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $news = $this->repositry->find($id);
        $newsCategories = $this->newsCategoryRepository->all(["id","name","parent_id"]);
        $newsCategories=  $this->newsCategoryRepository->sort($newsCategories);

        return view("admin.news.edit",["news"=>$news,"newsCategories"=>$newsCategories]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->repositry->delete($id);
        return Redirect::to("admin/news");
    }
}
